import BaseComponent from "./BaseComponent";
import {Button as AntButton, Checkbox, Radio, Select, Tree, TreeSelect} from "antd";
import React from "react";
import SysDataPresenter from "../../.gen/presenter/SysDataPresenter";

export default class RTreeSelect extends BaseComponent {

    static defaultProps = {
        id: undefined,
        pid: 0,
        valueField:'value',
        labelField:'title',
        parentCode:'0'
    };

    constructor(props) {
        super(props);
        this.state = {
            treeData: [],
            value: undefined,
        }
        this.presenter = new SysDataPresenter(this);
        let parentCode = this.props.parentCode;
        if (parentCode && !this.props.children) {
            this.loadDictOptions(parentCode);
        }else{
            if (this.props.treeData){
                this.setState({treeData:this.props.treeData})
            }
        }
    }

    // 根据字典code查询字典项
    loadDictOptions(parentCode) {
        this.presenter.findDictionaryList({dictCode: parentCode, model: this.props.model}, undefined, (res) => {
            if (res.code === 0) {
                let data= res.data;
                for (let i = 0; i < data.length; i++) {
                    data[i].key = data[i].dicCode;
                    data[i].title = data[i].dicName;
                }
                this.setState({"treeData":data});
            } else {
                console.error('getDictItems error: : ', res)
            }
        })
    }

    updateTreeData(list, key, children) {
        return list.map(node => {
            if (node.key === key) {
                return {
                    ...node,
                    children,
                };
            }
            if (node.children) {
                return {
                    ...node,
                    children: this.updateTreeData(node.children, key, children),
                };
            }
            return node;
        });
    }


    onLoadData = ({key, children}) =>
        new Promise(resolve => {
            if (children) {
                resolve();
                return;
            }
            this.presenter.findDictionaryList({dictCode: key, model: this.props.model}, undefined, (res) => {
                if (res.code === 0) {
                    let data= res.data;
                    for (let i = 0; i < data.length; i++) {
                        data[i].key = data[i].dicCode;
                        data[i].title = data[i].dicName;
                    }
                    this.setState({
                        treeData: this.updateTreeData(this.state.treeData, key,data)
                    })
                    resolve();
                } else {
                    console.error('getDictItems error: : ', res)
                }
            })
        });

    render() {
        let {treeData} = this.state;
        let props = {...this.props};
        delete props.parentCode;
        delete props.dictTable;
        return (
            <TreeSelect
                {...props}
                showSearch={true}
                style={{ width: '100%' }}
                dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
                placeholder="请选择"
                loadData={this.onLoadData}
                treeData={treeData}
            />
        )
    }
}